var mysql      = require('mysql')

//  1.创建连接
var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'bob',
  password : 'secret'
})

//  2.连接数据库
connection.connect(function(err) {
  if (err) {
    console.error('error connecting: ' + err.stack)
    return
  }
 
  console.log('connected as id ' + connection.threadId)
})

//  3.执行数据库
connection.query('SELECT 1', function (error, results, fields) {
    if (error) throw error;
    // connected!
});

//  4.关闭连接
connection.end()